package com.dayatang.android.Activity;

import java.util.concurrent.ExecutionException;

import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Dialog;
import android.view.View;

import com.dayatang.android.Chart.AbstractChart;

public class ProjectOutputallByMonthActivity extends AbstractChart{

	private String[] projectName = null;
	private String[] projectOutput = null;
	
	protected void InitData(){
		
		String url = getServerAddress() + "/remote/project-outputall-bymonth.action?fromYear="+fromYear+"&toYear="+toYear+"&fromMonth="+fromMonth+"&toMonth="+toMonth;
		
		try {
			String result = CheckServerData(url);
			
			if(result != null){
				
				JSONArray array = new JSONArray(result);

				projectName = new String[array.length()];
				projectOutput = new String[array.length()];

				for (int i = 0, len = array.length(); i < len; i++) {

					JSONObject jsonObject = array.getJSONObject(i);
					projectName[i] = jsonObject.getString("projectX");
					projectOutput[i] = jsonObject.getString("projectOutput");

				}
				FillContentView(execute());
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		
	}
	
	public View execute() {
		
		XYMultipleSeriesRenderer renderer = BuildBarRenderer(getResources().getColor(R.color.report_three));
		
		setChartSettings(renderer,getString(R.string.reports_tab_three)+getString(R.string.reports_statistics),getString(R.string.reports_x_axis_bymonth),getString(R.string.reports_y_axis_output),0.5,8.5,0,getArrayMax(projectOutput)*1.1);
		
	    for (int i = 1, len = projectOutput.length; i <= len; i++ ) {
	    	renderer.addXTextLabel(i, projectName[i-1]);
	    }
	    
		return ChartFactory.getBarChartView(this, BuildBarDataset(projectName,projectOutput), renderer, Type.DEFAULT);
	}
	
	protected Dialog onCreateDialog(int id) {
		
		
		
		
		return super.onCreateDialog(id);
	}
}
